var express = require("express");
var router = express.Router();
const {
  get,
  createCart,
  getOneCart,
  changeCart,
  delCart,
} = require("../api/carts");

//get请求获取优惠券数据和页面相关的操作
router.get("/", async function (req, res, next) {
  const { curPage, eachPage } = req.query;
  const data = await get({ curPage, eachPage });
  res.send(data);
});
//新增购物车(用户注册后自动新增一个空购物车)
router.post("/", async function (req, res, next) {
  const { detail } = req.body;
  const data = await createCart({ detail });
  res.send(data);
});
//通过id来获取购物车数据
router.get("/:_id", async function (req, res, next) {
  const { _id } = req.query;
  const data = await getOneCart(_id);
  res.send(data);
});
//修改优惠券数据(购物车内商品的增加、删除、修改)
router.put("/change", async function (req, res, next) {
  const newData = req.body;
  const data = await changeCart(newData);
  res.send(data);
});
//删除购物车(用户账户注销时调用)
router.delete("/del", async function (req, res, next) {
  const { _id } = req.body;
  const data = await delCart(_id);
  res.send(data);
});
module.exports = router;
